﻿using Microsoft.Win32;
using VisualWip.Core;
using VisualWip.Core.Interfaces;

namespace VisualWip.FilePersistanceProvider
{
    public class FileDialogPathSelector : IPathSelector
    {
        public FileDialogPathSelector()
        {
            DefaultExtension = ".xml";
            DefaultName = "settings";
            Filter = "Xml documents (.xml)|*.xml";
        }

        public string DefaultExtension { get; set; }
        public string Filter { get; set; }

        public string DefaultName { get; set; }

        #region IFileNameSelector Members

        public PathSelectorResult OpenSelector()
        {
            var dlg = new OpenFileDialog
                          {
                              FileName = DefaultName, DefaultExt = DefaultExtension, Filter = Filter
                          };

            bool? result = dlg.ShowDialog();
            return new PathSelectorResult(result.Value,new FileSettingsPath(){Path = dlg.FileName});
        }

        #endregion

        public PathSelectorResult SaveSelector()
        {
            var dlg = new SaveFileDialog();
            dlg.FileName = DefaultName;
            dlg.DefaultExt = DefaultName;
            dlg.Filter = Filter;

            bool? result = dlg.ShowDialog();
            return result.HasValue ? new PathSelectorResult(result.Value, new FileSettingsPath() { Path = dlg.FileName }) : new PathSelectorResult();
        }
    }
}